<?php
namespace Wap\Controller;
use Common\Model\CommentModel;
use Common\Model\BusinessModel;
use Common\Model\UserModel;
use Wap\BaseController;
use Common\Model\EventsModel;
use Common\Model\PointMallModel;
use Common\Model\UtilsModel;
class BusinessController extends BaseController {
    private $pageSize=10;
    //活动首页
    public function index(){
        $uid=session('uid')+0;

        if($this->isWechat) {
            //微信分享相关
            $this->smarty->assign('wxLocation', true);
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', '亲子商家');
            $this->smarty->assign('wxDesc', '你身边的优质亲子商家');
            $this->smarty->assign('wxLink', u('index'));
            $this->smarty->assign('wxImage', '');
            $this->smarty->assign('wxObjType',OBJ_TYPE_STORE);
            $this->smarty->assign('wxObjID',0);
        }
        

        //$data = BusinessModel::getStoreList(1,$this->pageSize,0,0,1,0,BusinessModel::SHOW_ORDER_STAR);

        $this->smarty->assign('title',C('SITE_NAME').'-亲子商家');
       // $this->smarty->assign('list',$data['data']);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);
        
        $ningbo='330200';
        $areaKv=UtilsModel::getReginKVByParent($ningbo);
        $this->smarty->assign('area',$areaKv);

        $tradeAreaKv=UtilsModel::getTradeAreaKV(0);
        $this->smarty->assign('tradeAreaKv',$tradeAreaKv);

        $businessItemKv=BusinessModel::getBusinessItemKv();
        $this->smarty->assign('businessItem',$businessItemKv);

        $this->smarty->display("Business/index.html");
    }

    //加载更多
    public function loadMore(){
        $page       = I('get.page',2,'intval');
        $districtId = I('get.districtId',0,'intval');
        $tradeArea  = I('get.tradeArea',0,'intval');
        $showOrder  = I('get.showOrder',0,'intval');
        $businessItem   = I('get.type',0,'intval');
        $lng            = I('get.lng',0,'float');
        $lat            = I('get.lat',0,'float');

        $data = BusinessModel::getStoreList($page,$this->pageSize,$districtId,$tradeArea,1,$businessItem,$showOrder,$lng,$lat);
        if($page<=1 && $data['total']==0){
            $data = BusinessModel::getStoreList(1,4,0,0,1,$businessItem,BusinessModel::SHOW_ORDER_DISTANCE,$lng,$lat);
            $this->smarty->assign('isNear',1);
        }

        $this->smarty->assign('list',$data['data']);
        $html = $this->smarty->fetch('Business/index_list.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$data['nextPage']));
    }

    //商家详情
    public function detail(){
        $uid=session('uid')+0;
        $id = I("get.id",0,'intval');
        if(!$id) redirect(U('Business/index'));//数据为空返回列表

        $info =BusinessModel::getStoreDetail($id);// response('Events/getEventsInfo', array('id'=>$id));
        //if($info['status'] != 0) redirect(U('Business/index'));//数据为空返回列表


        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('wxLocation', true);
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $info['storeName']);
            $this->smarty->assign('wxDesc',  $info['summary']);
            $this->smarty->assign('wxLink', $domain.U('detail?id=' . $id . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$info['listPic']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_STORE);
            $this->smarty->assign('wxObjID',$id);
        }



        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('loginuri',U("User/login")."&uri=".urlencode(U("Business/detail")."&id=$id"));
        $this->smarty->assign('title',$info['storeName'].'--'.C('SITE_NAME'));//$info['data']['title']
        $info['desc']=htmlspecialchars_decode($info['desc']);
        $this->smarty->assign('info',$info);

        $album=explode(',',$info['album']);
        $this->smarty->assign('album',$album);



        $package=BusinessModel::getPackageList(1,30,$info['restaurantId'],$info['id'],ENABLE_ENABLE);
        if($package['status']==0 ){
            $this->smarty->assign('package',$package['data']);
        }

        $this->smarty->assign('backURL',u('Business/index'));
        $this->smarty->assign('innerTitle','商家信息');

        $this->smarty->display("Business/detail.html");
    }

    public function packageDetail(){
        $id = I("get.id",0,'intval');
        $uid = session('uid')+0;
        $info=BusinessModel::getPackageInstance()->find($id);

        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $info['title']);
            $this->smarty->assign('wxDesc',  $info['summary']);
            $this->smarty->assign('wxLink', $domain.U('packageDetail?id='. $id . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$info['listPic']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_STORE_PACKAGE);
            $this->smarty->assign('wxObjID',$id);
        }

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('loginuri',U("User/login")."&uri=".urlencode(U("Business/packageDetail")."&id=$id"));
        $this->smarty->assign('title',$info['title'].'--'.C('SITE_NAME'));//$info['data']['title']
        $info['content']=htmlspecialchars_decode($info['content']);
        $this->smarty->assign('info',$info);

        $album=explode(',',$info['album']);
        $this->smarty->assign('album',$album);


        $this->smarty->assign('backURL',u('detail?id='.$info['storeId']));
        $this->smarty->assign('innerTitle','套餐详情');

        $this->smarty->display("Business/packageDetail.html");
    }
    /**
     * 评价
     */
    public function comment(){
        $objType=OBJ_TYPE_STORE;
        $objID=I('post.id',0,'int');
        $parentID=I('post.parentID',0,'int');//父级评论
        $content=I('post.content','','strip_tags');//
        $content=preg_replace('/@.*:/','',$content);
        $album=I('post.album');
        $albumstr=implode(',',$album);

        if(!session('uid')){
            $this->ajaxReturn(array('status'=>99, 'msg'=>'请先登录', 'uri'=>U('User/login')."&uri=".urlencode(U('Comment/comment')."&type=".$objType."&id=".$objID) ));
        }

        if(!$objType || !$objID){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'参数错误'));
        }

        if(true !== $maskStr = checkCleanWord($content) ){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'评论内容中不能包含'.$maskStr));
            return;
        }
        $uinfo = array(
            'uid'=>session('uid'),
            'nickname'=>session('nickname')
        );



        $data = CommentModel::commentNew($objType, $objID, $parentID, $content, $uinfo,0,$albumstr);
        $this->ajaxReturn($data);
    }


    public function feedback(){
        $id = I("get.id",0,'intval');

        $this->smarty->assign('backURL',u('detail?id='.$id));
        $this->smarty->assign('innerTitle','咨询留言');

        $this->smarty->assign('title',"给商家的咨询留言");
        $this->smarty->assign('id',$id);
        $this->smarty->display("Business/feedback.html");
    }


    /**
     * 获取评论列表
     */
    public function getComment(){
        $isSelf     = I('get.fixUser',0,'intval');
        $id         = I("get.id",0,'intval');
        $page       = I("get.page",1,'intval');//页码
        $pageSize   = I("get.pageSize",3,'intval');//


        $uid=session('uid')+0;
        $list=CommentModel::getCommentList(OBJ_TYPE_STORE,$id,$page,$pageSize,$isSelf,$uid);
        foreach($list['list'] as $k=>$v){
            $list['list'][$k]['album']=explode(',',$v['album']);
        }


        $this->smarty->assign('list',$list['list']);
        $html=$this->smarty->fetch("Business/feedback_list.html");


        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html,'isPage'=>$list['nextPage'],'total'=>$list['total']),'json',true);
    }


}